package com.java2207.group1.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java2207.group1.bo.OldManBo;
import com.java2207.group1.common.LayuiPageVo;
import com.java2207.group1.mapper.OldManMapper;
import com.java2207.group1.pojo.Case;
import com.java2207.group1.pojo.Cases;
import com.java2207.group1.pojo.OldMan;
import com.java2207.group1.service.OldManService;
import com.java2207.group1.vo.oldMan.OldManVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author HLJ
 * @Date 2022/7/26 07
 */
@Service("oldManService")
public class OldManServiceImpl implements OldManService {

    @Autowired
    private OldManMapper oldManMapper;

    @Override
    public LayuiPageVo<OldMan> oldManPageDate(Map<String, Object> params) {
        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");
        PageHelper.startPage(page, limit);

        List<OldMan> list = oldManMapper.oldManPageDate(params);

        PageInfo<OldMan> oldManPageInfo = new PageInfo<>(list);

        LayuiPageVo<OldMan> vo = new LayuiPageVo<>();

        vo.setCode(0);
        vo.setMsg("老人档案分页列表");
        vo.setCount(oldManPageInfo.getTotal()); // 获取总记录数
        vo.setData(oldManPageInfo.getList());
        return vo;
    }

    @Override
    public int addOldMan(OldMan oldMan) {
        return oldManMapper.addOldMan(oldMan);
    }
    @Override
    public int deleteOldMan(Integer id) {
        return oldManMapper.deleteOldMan(id);
    }

    @Override
    public int updateOldMan(OldMan oldMan) {
        return oldManMapper.updateOldMan(oldMan);
    }

    @Override
    public int removeOldMan(String ids) {
        String[] arrId = ids.split(",");
        return oldManMapper.removeOldMan(arrId);
    }

    /**
     * 给各年龄段老人分组，查询60-70,70-80,80-90 三个年龄段老人的人数
     *
     * @return
     */
    @Override
    public List<OldManVo> ageEchartBar() {
        OldManVo oldManVo1 = oldManMapper.ageEcharBar1();
        oldManVo1.setAge("60-70岁年龄段老人");
        OldManVo oldManVo2 = oldManMapper.ageEcharBar2();

        oldManVo2.setAge("70-80岁年龄段老人");
        OldManVo oldManVo3 = oldManMapper.ageEcharBar3();
        oldManVo3.setAge("80-90岁年龄段老人");

        List<OldManVo> list = new ArrayList<>();
        list.add(oldManVo1);
        list.add(oldManVo2);
        list.add(oldManVo3);
        return list;
    }

    @Override
    public LayuiPageVo<Cases> selectCaseHistory() {
        List<Cases> result = oldManMapper.selectCaseHistory();
        System.out.println(result);
        LayuiPageVo<Cases> vo = new LayuiPageVo<>();
        vo.setCode(0);
        vo.setMsg("病例信息");
        vo.setCount(vo.getCount());
        vo.setData(result);
        return vo;
    }


    @Override
    public List<OldManBo> staffExcelData(Map<String, Object> params) {
        List<OldMan> oldManList = oldManMapper.oldManPageDate(params);

        List<OldManBo> oldManBos = oldManList.stream().map((sta) -> {
            OldManBo oldManBo = new OldManBo();

            oldManBo.setId(sta.getId());
            oldManBo.setName(sta.getName());
            oldManBo.setSex(sta.getSex());
            oldManBo.setAge(sta.getAge());
            oldManBo.setTel(sta.getTel());
            oldManBo.setAddress(sta.getAddress());
            oldManBo.setGuardian(sta.getGuardian());
            oldManBo.setRelation(sta.getRelation());
            oldManBo.setGuardian_tel(sta.getGuardian_tel());
            oldManBo.setAnnihdate(sta.getAnnihdate());
            oldManBo.setDischarge_date(sta.getDischarge_date());
            oldManBo.setState(sta.getState());
            oldManBo.setPic(sta.getPic());
            return oldManBo;
        }).collect(Collectors.toList());

        return oldManBos;
    }

    @Override
    public OldMan selectOldMan(String tel) {
        return oldManMapper.selectOldMan(tel);
    }

}
